home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / LSEQ101.ZIP / LSINSERT.C < prev    next >
Text File  |  1990-06-20  |  4KB  |  154 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)lsinsert.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <string.h>*/
  9.  
  10. /* library headers */
  11. #include <blkio.h>
  12.  
  13. /* local headers */
  14. #include "lseq_.h"
  15.  
  16. /*man---------------------------------------------------------------------------
  17. NAME
  18.      lsinsert - insert record
  19.  
  20. SYNOPSIS
  21.      #include <lseq.h>
  22.  
  23.      int lsinsert(lsp, buf)
  24.      lseq_t *lsp;
  25.      const void *buf;
  26.  
  27. DESCRIPTION
  28.      The lsinsert function creates a new record following the current
  29.      record and writes the data pointed to by buf into that record.
  30.      The cursor is set to the inserted record.
  31.  
  32.      lsinsert will fail if one or more of the following is true:
  33.  
  34.      [EINVAL]       lsp is not a valid lseq pointer.
  35.      [EINVAL]       buf is the NULL pointer.
  36.      [LSELOCK]      lsp is not write locked.
  37.      [LSENOPEN]     lsp is not open.
  38.  
  39. SEE ALSO
  40.      lsdelcur, lsinsert, lssearch.
  41.  
  42. DIAGNOSTICS
  43.      Upon successful completion, a value of 0 is returned.  Otherwise,
  44.      a value of -1 is returned, and errno set to indicate the error.
  45.  
  46. ------------------------------------------------------------------------------*/
  47. int lsinsert(lsp, buf)
  48. lseq_t *lsp;
  49. const void *buf;
  50. {
  51.     int terrno = 0;
  52.     bpos_t bpos = NIL;
  53.  
  54.     /* validate arguments */
  55.     if (!ls_valid(lsp) || buf == NULL) {
  56.         errno = EINVAL;
  57.         return -1;
  58.     }
  59.  
  60.     /* check if not open */
  61.     if (!(lsp->flags & LSOPEN)) {
  62.         errno = LSENOPEN;
  63.         return -1;
  64.     }
  65.  
  66.     /* check if not write locked */
  67.     if (!(lsp->flags & LSWRLCK)) {
  68.         errno = LSELOCK;
  69.         return -1;
  70.     }
  71.  
  72.     /* build record to insert */
  73.     lsp->clsrp->prev = lsp->clspos;
  74.     if (lsp->clspos == NIL) {
  75.         lsp->clsrp->next = lsp->lshdr.first;
  76.     }
  77.     memcpy(lsp->clsrp->recbuf, buf, lsp->lshdr.recsize);
  78.  
  79.     /* set modify bit in header */
  80.     lsp->lshdr.flags |= LSHMOD;
  81.     if (bputhf(lsp->bp, sizeof(bpos_t),
  82.         (void *)((char *)&lsp->lshdr + sizeof(bpos_t)),
  83.             sizeof(lshdr_t) - sizeof(bpos_t)) == -1) {
  84.         LSEPRINT;
  85.         return -1;
  86.     }
  87.     if (bsync(lsp->bp) == -1) {
  88.         LSEPRINT;
  89.         return -1;
  90.     }
  91.  
  92.     /* get empty block from free list */
  93.     if (bflpop(lsp->bp, &bpos) == -1) {
  94.         LSEPRINT;
  95.         return -1;
  96.     }
  97.     lsp->clspos = bpos;
  98.  
  99.     /* write new record */
  100.     if (ls_rcput(lsp, lsp->clspos, lsp->clsrp) == -1) {
  101.         LSEPRINT;
  102.         terrno = errno;
  103.         bpos = lsp->clspos;
  104.         bflpush(lsp->bp, &bpos);
  105.         errno = terrno;
  106.         return -1;
  107.     }
  108.  
  109.     /* increment record count */
  110.     lsp->lshdr.reccnt++;
  111.  
  112.     /* fix links */
  113.     if (lsp->clsrp->next == NIL) {
  114.         lsp->lshdr.last = lsp->clspos;
  115.     } else {
  116.         if (bputbf(lsp->bp, (bpos_t)lsp->clsrp->next, offsetof(lsrec_t, prev), &lsp->clspos, sizeof(lsp->clsrp->prev)) == -1) {
  117.             LSEPRINT;
  118.             terrno = errno;
  119.             bpos = lsp->clspos;
  120.             bflpush(lsp->bp, &bpos);
  121.             errno = terrno;
  122.             return -1;
  123.         }
  124.     }
  125.     if (lsp->clsrp->prev == NIL) {
  126.         lsp->lshdr.first = lsp->clspos;
  127.     } else {
  128.         if (bputbf(lsp->bp, (bpos_t)lsp->clsrp->prev, offsetof(lsrec_t, next), &lsp->clspos, sizeof(lsp->clsrp->next)) == -1) {
  129.             LSEPRINT;
  130.             terrno = errno;
  131.             bpos = lsp->clspos;
  132.             bflpush(lsp->bp, &bpos);
  133.             errno = terrno;
  134.             return -1;
  135.         }
  136.     }
  137.  
  138.     /* clear modify bit in header */
  139.     lsp->lshdr.flags &= ~LSHMOD;
  140.     if (bputhf(lsp->bp, sizeof(bpos_t),
  141.         (void *)((char *)&lsp->lshdr + sizeof(bpos_t)),
  142.             sizeof(lshdr_t) - sizeof(bpos_t)) == -1) {
  143.         LSEPRINT;
  144.         return -1;
  145.     }
  146.     if (bsync(lsp->bp) == -1) {
  147.         LSEPRINT;
  148.         return -1;
  149.     }
  150.  
  151.     errno = 0;
  152.     return 0;
  153. }
  154.